<?php

namespace app\model\order;

use app\base\BaseModel;

/**
 * 模型-OrderPtModel
 * Class OrderPtModel
 * @package app\model
 */
class OrderRebateModel extends BaseModel
{

    protected $name = 'order_rebate';

    /**
     * @param $rebate
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addRebate($rebate)
    {
        $ret_arr = $this->saveData($rebate);
        if ($ret_arr['flag'] === false) {
            return false;
        }
        return true;
    }

    /**
     * 获取
     * @param $id
     * @param $field
     * @param $time
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getStatisticsMoney($id, $field, $time)
    {
        $rebate = $this->where($field, $id)->where('end_time', 'elt', $time)
            ->field("SUM($field) as money")->find();
        if (empty($rebate)) {
            return 0;
        }
        return $rebate['money'];
    }

    /**
     * 结算金额
     * @param $map
     * @return bool
     */
    public function settlement($map)
    {
        $up_data = [
            'status' => OrderRebateModel::$Status['settlement'],
            'update_time' => time()
        ];
        $ret = $this->where($map)->update($up_data);
        if ($ret === false) {
            return false;
        }
        return true;
    }

    /**
     * 0未结算  1已结算  2已退款
     * @var array
     */
    public static $Status = [
        'noSettlement' => [
            'code' => 0,
            'name' => "未结算"
        ],
        'settlement' => [
            'code' => 1,
            'name' => "已结算"
        ],
        'refund' => [
            'code' => 2,
            'name' => "已退款"
        ]
    ];

}
